package com.znf4.controller.good;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.znf4.constants.CommonConst;
import com.znf4.good.enums.GoodStatusEnum;
import com.znf4.enums.OperationStatusEnum;
import com.znf4.good.jifen.entity.Jifen;
import com.znf4.good.jifen.server.JifenServer;
import com.znf4.id.IdGenerator;
import com.znf4.utils.OSSFile;

/**
 * 商品信息
 */
@Controller
@RequestMapping("/good/jifen")
public class JifenGoodController {

	private static final Logger LOG = LoggerFactory.getLogger(JifenGoodController.class);

	@Autowired
	private JifenServer jifenServer;

	@Autowired
	private IdGenerator idGenerator;

	/**
	 * 函数功能说明 ： 添加积分商品
	 *
	 * @参数： @return @return String @throws
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/add", method = { RequestMethod.POST, RequestMethod.GET })
	public String add(Jifen jifen, Model model, HttpServletRequest request) {

		try {
			jifen.setId(idGenerator.getGoodNameNumber());

			String fileName = CommonConst.JIFEN_GOOD_IMG_NUMBER + jifen.getId();

			// 文件上传OSS
			Iterator iter = OSSFile.getFileIterator(request);
			if (iter != null) {
				// 将request变成多部分request
				MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
				// 获取multiRequest 中所有的文件名
				while (iter.hasNext()) {
					// 一次遍历所有文件
					MultipartFile file = multiRequest.getFile(iter.next().toString());
					jifen.setImg(OSSFile.upFile(file, fileName));
					LOG.info("积分商品上传图片到oss.....并设置数据库参数。");
				}
			}
			jifen.setStatus(GoodStatusEnum.FROZEN.getDesc()); // 状态
			jifenServer.addJifenGood(jifen);

			model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc());
		} catch (Exception e) {
			LOG.info(e.getMessage());
			e.printStackTrace();
			model.addAttribute("status", OperationStatusEnum.FAILED.getDesc());
		}
		return "good/list";

	}

	/**
	 * 函数功能说明 ： 修改积分商品
	 *
	 * @参数： @return @return String @throws
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/update", method = { RequestMethod.POST, RequestMethod.GET })
	public String update(Jifen jifen, Model model, HttpServletRequest request) {

		try {
			String fileName = CommonConst.JIFEN_GOOD_IMG_NUMBER + jifen.getId();
			// 文件上传OSS
			Iterator iter = OSSFile.getFileIterator(request);
			if (iter != null) {
				// 将request变成多部分request
				MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
				// 获取multiRequest 中所有的文件名
				while (iter.hasNext()) {
					// 一次遍历所有文件
					MultipartFile file = multiRequest.getFile(iter.next().toString());
					jifen.setImg(OSSFile.upFile(file, fileName));
				}
			}
			jifenServer.updateJifenGood(jifen);
			model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc());

		} catch (Exception e) {
			LOG.info("修改免费商品信息 :", e.getMessage());
			e.printStackTrace();
			model.addAttribute("status", OperationStatusEnum.FAILED.getDesc());
		}

		return "good/list";
	}

	/**
	 * 函数功能说明 ： 删除积分商品
	 *
	 * @参数： @return @return String @throws
	 */
	@RequestMapping(value = "/delOne", method = { RequestMethod.POST, RequestMethod.GET })
	public String delOne(Jifen jifen, Model model) {

		try {
			jifenServer.delOneJifenGood(jifen);
			// 删除文件 OSS
			OSSFile.deleteFile(jifen.getImg());
			model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc());
		} catch (Exception e) {
			LOG.info("删除单个其他图片 :", e.getMessage());
			e.printStackTrace();
			model.addAttribute("status", OperationStatusEnum.FAILED.getDesc());
		}
		return "good/list";
	}

	/**
	 * 函数功能说明 ： 删除多条积分商品
	 *
	 * @参数： @return @return String @throws
	 */
	@RequestMapping(value = "/delAll", method = { RequestMethod.POST, RequestMethod.GET })
	public String delAll(String[] jifens, Model model) {
		try {
			List<Jifen> list = new ArrayList<>();
			for (int i = 0; i < jifens.length; i++) {

				Jifen jifen = new Jifen();
				jifen.setId(jifens[i]);
				list.add(jifen);

				// 删除文件
				Jifen delJifen = jifenServer.queryOne(jifen);
				OSSFile.deleteFile(delJifen.getImg());

			}
			jifenServer.delAllJifenGood(list);
			model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc());

		} catch (Exception e) {
			LOG.info("删除多个其他商品信息:", e.getMessage());
			e.printStackTrace();
			model.addAttribute("status", OperationStatusEnum.FAILED.getDesc());
		}
		return "good/list";
	}
	
	
	
	
	

}
